﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyDiemDHBK
{
    public partial class SuaThongTin : Form
    {
        string stt, masv, hoten, lop, hocky, monhoc, diemcc, diemgk, diemck;
        public static string sql = "";

        public SuaThongTin(string stt, string masv, string hoten, string lop, string monhoc, string diemcc, string diemgk, string diemck, string hocky)
        {
            this.stt = stt;
            this.masv = masv;
            this.hoten = hoten;
            this.lop = lop;
            this.hocky = hocky;
            this.monhoc = monhoc;
            this.diemcc = diemcc;
            this.diemgk = diemgk;
            this.diemck = diemck;
            InitializeComponent();
        }

        private void SuaThongTin_Load(object sender, EventArgs e)
        {
            lbmasv.Text = "Mã SV:  "+masv;
            lblop.Text = lop;
            lbhoten.Text = "Họ tên:  "+hoten;
            cbbhocky.Text = hocky;
            cbbmonhoc.Text = monhoc;
            tbdiemcc.Text = diemcc;
            tbdiemgk.Text = diemgk;
            tbdiemck.Text = diemck;
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btok_Click(object sender, EventArgs e)
        {
            string hocky = cbbhocky.Text;
            string monhoc = cbbmonhoc.Text;
            double diemcc = Convert.ToDouble(tbdiemcc.Text);
            double diemgk = Convert.ToDouble(tbdiemgk.Text);
            double diemck = Convert.ToDouble(tbdiemck.Text);
            if (diemcc < 0 || diemgk < 0 || diemck < 0 || diemcc > 10 || diemgk > 10 || diemck > 10)
                MessageBox.Show("Bạn đã nhập điểm sai..!");
            else if (MessageBox.Show("Bạn có chắc các thông tin muốn sửa..?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string diemchu = "";
                    float diemphay = 0;
                    double tinhdiem = diemcc * 0.2 + diemgk * 0.3 + diemck * 0.5;
                    diemphay = (float)tinhdiem;
                    if (diemphay < 4) diemchu = "F";
                    else if (diemphay >= 4 && diemphay <= 5.4) diemchu = "D";
                    else if (diemphay > 5.4 && diemphay < 7) diemchu = "C";
                    else if (diemphay >= 7 && diemphay < 8.5) diemchu = "B";
                    else diemchu = "A";

                    sql = "update Diem set DiemCC = '" + diemcc + "', DiemGK = '" + diemgk + "', DiemCK = '" + diemck + 
                        "', DiemChu = '" + diemchu + "',DiemPhay = '" + diemphay + "', MaHK =  (select MaHK from  HocKy where TenHK like N'" + hocky + 
                        "') where STT = '"+stt+"'";
                    this.Dispose();
                }
        }

        public string getsql()
        {
            return sql;
        }

        private void tbdiemcc_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8 || e.KeyChar == 46);
        }

        private void tbdiemgk_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8 || e.KeyChar == 46);
        }

        private void tbdiemck_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8 || e.KeyChar == 46);
        }
    }
}
